package com.wyy.controller;

import com.alibaba.fastjson.JSON;
import com.wyy.pojo.UserPojo;
import com.wyy.pojo.Users;
import com.wyy.response.CommonReturnType;
import com.wyy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: gytcb
 * @author: 吴磊
 * @create: 2021-06-07 10:54
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 获取所有的用户
     *
     * @param curPage
     * @return
     */
    @RequestMapping(value = "/getAllUser", method = RequestMethod.POST)
    public CommonReturnType getAllUser(@RequestBody String curPage) {
        String page = JSON.parseObject(curPage, UserPojo.class).getCurPage();
        Map<String, Object> map = userService.getAllUsers(Integer.parseInt(page));
        return CommonReturnType.create(map);
    }

    /**
     * 更新用户的数据
     *
     * @param users
     * @return
     */
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public CommonReturnType updateUser(@RequestBody Users users) {
        userService.updateUser(users);
        return CommonReturnType.create(null);
    }

    /**
     * 删除用户
     *
     * @param users
     * @return
     */
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public CommonReturnType deleteUser(@RequestBody Users users) {
        userService.deleteUser(users);
        return CommonReturnType.create(null);
    }

    /**
     * 根据ID获取用户
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getUserAdminById", method = RequestMethod.POST)
    public CommonReturnType getUserById(@RequestBody String userId) {
        Map<String, Object> map = userService.getUserByUserId(userId);
        return CommonReturnType.create(map);
    }

    /**
     * 管理员登录
     *
     * @param users
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonReturnType login(@RequestBody Users users) {
        Map<String, Object> map = userService.login(users);
        return CommonReturnType.create(map);
    }


}
